package com.objetdirect.gwt.umldrawer.client;

import com.google.gwt.user.client.History;
import com.gwtext.client.core.EventObject;
import com.gwtext.client.data.ArrayReader;
import com.gwtext.client.data.FieldDef;
import com.gwtext.client.data.MemoryProxy;
import com.gwtext.client.data.RecordDef;
import com.gwtext.client.data.Store;
import com.gwtext.client.data.StringFieldDef;
import com.gwtext.client.widgets.Button;
import com.gwtext.client.widgets.Panel;
import com.gwtext.client.widgets.Toolbar;
import com.gwtext.client.widgets.ToolbarButton;
import com.gwtext.client.widgets.event.ButtonListenerAdapter;
import com.gwtext.client.widgets.grid.BaseColumnConfig;
import com.gwtext.client.widgets.grid.CheckboxColumnConfig;
import com.gwtext.client.widgets.grid.CheckboxSelectionModel;
import com.gwtext.client.widgets.grid.ColumnConfig;
import com.gwtext.client.widgets.grid.ColumnModel;
import com.gwtext.client.widgets.grid.GridPanel;
import com.gwtext.client.widgets.layout.HorizontalLayout;
import com.gwtext.client.widgets.layout.VerticalLayout;
import com.gwtext.client.widgets.tree.TreePanel;

public class MyProjectsWorkSpace {

	private Panel wholePanel = new Panel();
	private TreePanel projects = new TreePanel();
	private Panel projectMembers = new Panel();

	public Panel getMyProjects() {

		projects.setTitle("My Projects");
		projects.setWidth(460);
		projects.setHeight(500);
		projects.setCollapsible(true);
		projects.setTitleCollapse(true);
		projects.setAutoScroll(true);
		projects.setRootNode(new ProjectsTree().getProjectsTree());
		projects.setStyleName("projectsTree");
		projects.expandAll();
		Toolbar projectsToolbar = new Toolbar();
		
		ToolbarButton createProject = new ToolbarButton("Create New");
		createProject.setIcon("images/createProject.png");
		projectsToolbar.addButton(createProject);

		ButtonListenerAdapter createProjectAdapter = new ButtonListenerAdapter() {
			@Override
			public void onClick(Button button, EventObject e) {
				new CreateProjectWindow();
			}
		};
		createProject.addListener(createProjectAdapter);

		ToolbarButton leaveProject = new ToolbarButton("Leave Project");
		leaveProject.setIcon("images/leaveProject.png");
		projectsToolbar.addButton(leaveProject);

		ButtonListenerAdapter leaveProjectAdapter = new ButtonListenerAdapter() {
			@Override
			public void onClick(Button button, EventObject e) {
			}
		};
		leaveProject.addListener(leaveProjectAdapter);

		projectsToolbar.addSeparator();
		
		ToolbarButton members = new ToolbarButton("Members");
		members.setIcon("images/members.png");
		projectsToolbar.addButton(members);

		ButtonListenerAdapter membersAdapter = new ButtonListenerAdapter() {
			@Override
			public void onClick(Button button, EventObject e) {
			}
		};
		members.addListener(membersAdapter);

		ToolbarButton notesProject = new ToolbarButton("Edit Notes");
		notesProject.setIcon("images/editNotes.png");
		projectsToolbar.addButton(notesProject);

		ButtonListenerAdapter notesProjectAdapter = new ButtonListenerAdapter() {
			@Override
			public void onClick(Button button, EventObject e) {
			}
		};
		notesProject.addListener(notesProjectAdapter);

		projectsToolbar.addSeparator();
		
		ToolbarButton openProject = new ToolbarButton("Open Project");
		openProject.setIcon("images/openProject.png");
		projectsToolbar.addButton(openProject);

		ButtonListenerAdapter openProjectAdapter = new ButtonListenerAdapter() {
			@Override
			public void onClick(Button button, EventObject e) {
				History.newItem("UMLDrawer");
			}
		};
		openProject.addListener(openProjectAdapter);

		projects.setBottomToolbar(projectsToolbar);

		/**********************************************************************************
		 * Project Members' Panel
		 */

		projectMembers.setLayout(new VerticalLayout(0));

		final CheckboxSelectionModel cbSelectionModel = new CheckboxSelectionModel();

		RecordDef recordDef = new RecordDef(new FieldDef[] {
				new StringFieldDef("name"), new StringFieldDef("e-mail"),
				new StringFieldDef("privilege") });

		GridPanel grid = new GridPanel();

		Object[][] data = getCompanyData();
		MemoryProxy proxy = new MemoryProxy(data);

		ArrayReader reader = new ArrayReader(recordDef);
		Store store = new Store(proxy, reader);
		store.load();
		grid.setStore(store);

		BaseColumnConfig[] columns = new BaseColumnConfig[] {
				new CheckboxColumnConfig(cbSelectionModel),
				new ColumnConfig("Name", "name", 188, true, null, "name"),
				new ColumnConfig("E-mail", "e-mail", 186),
				new ColumnConfig("Privilege", "privilege", 50) };

		ColumnModel columnModel = new ColumnModel(columns);
		grid.setColumnModel(columnModel);

		grid.setSelectionModel(cbSelectionModel);
		grid.setSize(448, 447);

		projectMembers.setWidth(450);
		projectMembers.setHeight(500);
		projectMembers.setCollapsible(true);
		projectMembers.setTitleCollapse(true);
		projectMembers.setAutoScroll(true);
		projectMembers.setBorder(true);
		projectMembers.setTitle("Project Members");

		projectMembers.add(grid);
		Toolbar projectMembersToolbar = new Toolbar();

		ToolbarButton addMember = new ToolbarButton("Add Member");
		addMember.setIcon("images/addMember.png");
		projectMembersToolbar.addButton(addMember);

		ButtonListenerAdapter addMemberAdapter = new ButtonListenerAdapter() {
			@Override
			public void onClick(Button button, EventObject e) {
				new AddMemberWindow();
			}
		};
		addMember.addListener(addMemberAdapter);
		
		projectMembersToolbar.addSeparator();

		ToolbarButton removeMember = new ToolbarButton("Remove Member");
		removeMember.setIcon("images/removeMember.png");
		projectMembersToolbar.addButton(removeMember);

		ButtonListenerAdapter removeMemberAdapter = new ButtonListenerAdapter() {
			@Override
			public void onClick(Button button, EventObject e) {
			}
		};
		removeMember.addListener(removeMemberAdapter);
		
		projectMembersToolbar.addSeparator();

		ToolbarButton editMember = new ToolbarButton("Edit Member");
		editMember.setIcon("images/editMember.png");
		projectMembersToolbar.addButton(editMember);

		ButtonListenerAdapter editMemberAdapter = new ButtonListenerAdapter() {
			@Override
			public void onClick(Button button, EventObject e) {
				new EditMemberPrivilegeWindow();
			}
		};
		editMember.addListener(editMemberAdapter);

		projectMembersToolbar.addSeparator();
		
		ToolbarButton composeMessage = new ToolbarButton("Compose Message");
		composeMessage.setIcon("images/composeMessage.png");
		projectMembersToolbar.addButton(composeMessage);

		ButtonListenerAdapter composeMessageAdapter = new ButtonListenerAdapter() {
			@Override
			public void onClick(Button button, EventObject e) {
				new MessageFormWindow().showMessageFormWindow();
			}
		};
		composeMessage.addListener(composeMessageAdapter);

		projectMembers.setBottomToolbar(projectMembersToolbar);

		wholePanel.setLayout(new HorizontalLayout(10));
		wholePanel.add(projects);
		wholePanel.add(projectMembers);

		return wholePanel;
	}

	private Object[][] getCompanyData() {
		return new Object[][] {
				new Object[] { "Andrew Aziz", "eng.andrew_aziz@hotmail.com",
						"R/W" },
				new Object[] { "Sandra Sabry", "sando_elking@hotmail.com",
						"R/W" },
				new Object[] { "Bakoum Nasr", "pepo_20070099@yahoo.com",
						"R/W" },
				new Object[] { "Mark Alfons", "mark.alfons@yahoo.com", "R/W" }, };
	}
}